package com.example.springmvcdemo.controller;

import com.example.springmvcdemo.model.MessageInfo;
import com.example.springmvcdemo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    private List<MessageInfo> messageInfos = new ArrayList<>();

    @RequestMapping("/publish")
    public boolean publishMessage(MessageInfo messageInfo) {
        if (!StringUtils.hasLength(messageInfo.getMessage())
        || !StringUtils.hasLength(messageInfo.getFrom())
        || !StringUtils.hasLength(messageInfo.getTo())) {
            return false;
        }
        // 暂存内存
//        messageInfos.add(messageInfo);

        // 把数据放在 mysql 当中
        messageService.insertMessage(messageInfo);

        return true;
    }

    @RequestMapping("/getList")
    public List<MessageInfo> getList() {
        return messageService.quaryList();
    }
}
